using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Scene;
using MicroMsg.Scene.Voice;
using MicroMsg.Storage;
using System;
using System.IO;
using System.Text;
namespace MicroMsg.Plugin.MassSend.Scene
{
	public class MassSendContext : IContextBase, IRecorderContext
	{
		public delegate void onStopDelegate(byte[] voiceBuffer, int dataLength, int timeLength);
		private const string TAG = "MassSendContext";
		public const int MAX_TIMES = 30;
		public MassMsg mMassMsgInfo;
		public byte[] mDatabuf;
		public byte[] mThumbDatabuf;
		public int mStartPos;
		public int mThumbStartPos;
		public int nCameraType;
		public int nVideoSource;
		private int mSendDataLength = 8192;
		private int mSendThumbDataLength = 8192;
		public bool bIsCancelled;
		public uint mTimesToServer;
		public event MassSendContext.onStopDelegate mOnStop;
		public MassSendContext()
		{
			this.mMassMsgInfo = new MassMsg();
			this.mMassMsgInfo.nStatus = 0;
		}
		public bool isRunning()
		{
			if (this.mMassMsgInfo.nStatus != 0 && this.mMassMsgInfo.nStatus != 1)
			{
				if (this.mMassMsgInfo.nStatus != 2)
				{
					return false;
				}
			}
			return true;
		}
		public bool needToHandle()
		{
			return true;
		}
		public bool needToClean()
		{
			return true;
		}
		public void onStartup(int creatTime, object args)
		{
		}
		public void onVoiceTimeChanged(int timeLength)
		{
		}
		public void onStop(byte[] voiceBuffer, int dataLength, int timeLength)
		{
			if (this.mOnStop == null)
			{
				return;
			}
			this.mOnStop(voiceBuffer, dataLength, timeLength);
		}
		public bool isNeedData()
		{
			return true;
		}
		public bool onAppendOutputData(byte[] buffer, int offset, int count)
		{
			return true;
		}
		public bool packText(string strUserNameList, uint nUserCount, bool bIsSendAgain, string strContent)
		{
			this.mMassMsgInfo.strUserNameList = strUserNameList;
			this.mMassMsgInfo.nUserCount = nUserCount;
			this.mMassMsgInfo.strClientMsgId = MD5Core.GetHashString(this.mMassMsgInfo.strUserNameList + Util.getNowMilliseconds());
			this.mMassMsgInfo.strContent = strContent;
			this.mDatabuf = Encoding.get_UTF8().GetBytes(strContent);
			this.mMassMsgInfo.nDataTotalLength = (uint)this.mDatabuf.Length;
			this.mMassMsgInfo.nMsgType = 1u;
			this.mMassMsgInfo.bIsSendAgain = bIsSendAgain;
			return true;
		}
		public bool packImage(string strUserNameList, uint nUserCount, bool bIsSendAgain, Stream origStream, bool isNeedScale)
		{
			this.mMassMsgInfo.strUserNameList = strUserNameList;
			this.mMassMsgInfo.nUserCount = nUserCount;
			this.mMassMsgInfo.strClientMsgId = MD5Core.GetHashString(this.mMassMsgInfo.strUserNameList + Util.getNowMilliseconds());
			this.mMassMsgInfo.strFileName = StorageIO.getPluginPath("MassSend") + "/image/img_" + MD5Core.GetHashString(this.mMassMsgInfo.strUserNameList + Util.getNowMilliseconds()) + ".jpg";
			this.mMassMsgInfo.strThumbFileName = StorageIO.getPluginPath("MassSend") + "/thumbnail/img_Thumb_" + MD5Core.GetHashString(this.mMassMsgInfo.strUserNameList + Util.getNowMilliseconds()) + ".jpg";
			this.mMassMsgInfo.nMsgType = 3u;
			this.mMassMsgInfo.bIsSendAgain = bIsSendAgain;
			this.mMassMsgInfo.isNeedScale = isNeedScale;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				if (!Util.zoomCompressImage(origStream, memoryStream, isNeedScale, 960, 960, 70, 56320))
				{
					Log.smethod_1("MassSendContext", "zoomCompressImage failed,big image ");
					bool result = false;
					return result;
				}
				this.mDatabuf = memoryStream.ToArray();
				this.mMassMsgInfo.nDataTotalLength = (uint)this.mDatabuf.Length;
			}
			using (MemoryStream memoryStream2 = new MemoryStream())
			{
				if (!Util.zoomCompressImage(origStream, memoryStream2, true, 100, 100, 70, 56320))
				{
					Log.smethod_1("MassSendContext", "zoomCompressImage failed, thumb image");
					bool result = false;
					return result;
				}
				this.mThumbDatabuf = memoryStream2.ToArray();
				this.mMassMsgInfo.nThumbTotalLength = (uint)this.mThumbDatabuf.Length;
			}
			return true;
		}
		public bool packVoice(byte[] voiceBuffer, int dataLength, int timeLength)
		{
			this.mDatabuf = new byte[dataLength];
			Buffer.BlockCopy(voiceBuffer, 0, this.mDatabuf, 0, dataLength);
			this.mMassMsgInfo.nDataTotalLength = (uint)this.mDatabuf.Length;
			this.mMassMsgInfo.nMediaTime = timeLength;
			this.mMassMsgInfo.strClientMsgId = MD5Core.GetHashString(this.mMassMsgInfo.strUserNameList + Util.getNowMilliseconds());
			this.mMassMsgInfo.strFileName = StorageIO.getPluginPath("MassSend") + "/voice/voice_" + MD5Core.GetHashString(this.mMassMsgInfo.strUserNameList + Util.getNowMilliseconds()) + ".amr";
			this.mMassMsgInfo.nMsgType = 34u;
			return true;
		}
		public bool packVideo()
		{
			return true;
		}
		public SKBuiltinBuffer_t getRemainThumbDataToSend()
		{
			if (this.mMassMsgInfo.nMsgType != 43u)
			{
				return Util.toSKBuffer("");
			}
			if (this.mThumbStartPos < 0)
			{
				return Util.toSKBuffer("");
			}
			if (this.mMassMsgInfo.nThumbTotalLength <= 0u)
			{
				return Util.toSKBuffer("");
			}
			if ((long)this.mThumbStartPos >= (long)((ulong)this.mMassMsgInfo.nThumbTotalLength))
			{
				return Util.toSKBuffer("");
			}
			if (this.mThumbDatabuf == null)
			{
				return Util.toSKBuffer("");
			}
			this.mSendThumbDataLength = Util.getSendPackSize();
			if ((ulong)this.mMassMsgInfo.nThumbTotalLength - (ulong)((long)this.mThumbStartPos) < (ulong)((long)this.mSendThumbDataLength))
			{
				this.mSendThumbDataLength = (int)(this.mMassMsgInfo.nThumbTotalLength - (uint)this.mThumbStartPos);
			}
			return Util.toSKBuffer(this.mThumbDatabuf, this.mThumbStartPos, this.mSendThumbDataLength);
		}
		public SKBuiltinBuffer_t getRemainDataToSend()
		{
			if (this.mStartPos < 0)
			{
				return Util.toSKBuffer("");
			}
			if (this.mMassMsgInfo.nDataTotalLength <= 0u)
			{
				return Util.toSKBuffer("");
			}
			if ((long)this.mStartPos >= (long)((ulong)this.mMassMsgInfo.nDataTotalLength))
			{
				return Util.toSKBuffer("");
			}
			if (this.mDatabuf == null)
			{
				return Util.toSKBuffer("");
			}
			this.mSendDataLength = Util.getSendPackSize();
			if ((ulong)this.mMassMsgInfo.nDataTotalLength - (ulong)((long)this.mStartPos) < (ulong)((long)this.mSendDataLength))
			{
				this.mSendDataLength = (int)(this.mMassMsgInfo.nDataTotalLength - (uint)this.mStartPos);
			}
			return Util.toSKBuffer(this.mDatabuf, this.mStartPos, this.mSendDataLength);
		}
		public bool isSendCompleted()
		{
			if (this.mMassMsgInfo.nMsgType == 1u)
			{
				return true;
			}
			if (this.mMassMsgInfo.nMsgType != 3u)
			{
				if (this.mMassMsgInfo.nMsgType != 34u)
				{
					if (this.mMassMsgInfo.nMsgType == 43u)
					{
						return true;
					}
					Log.smethod_1("MassSendContext", "invalid msgtype = " + this.mMassMsgInfo.nMsgType);
					return true;
				}
			}
			this.mStartPos += this.mSendDataLength;
			return (long)this.mStartPos == (long)((ulong)this.mMassMsgInfo.nDataTotalLength);
		}
		public bool saveInfo()
		{
			if (!StorageMgr.massMsg.update(this.mMassMsgInfo))
			{
				Log.smethod_1("MassSendContext", "update mMassMsgInfo to DB failed!");
				return false;
			}
			if (this.mMassMsgInfo.nMsgType == 1u)
			{
				return true;
			}
			if (this.mMassMsgInfo.nMsgType == 34u)
			{
				UTF8Encoding uTF8Encoding = new UTF8Encoding();
				byte[] bytes = uTF8Encoding.GetBytes("#!AMR\n");
				byte[] array = new byte[this.mDatabuf.Length + bytes.Length];
				Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length);
				Buffer.BlockCopy(this.mDatabuf, 0, array, bytes.Length, this.mDatabuf.Length);
				StorageIO.writeToFile(this.mMassMsgInfo.strFileName, array, false);
			}
			else
			{
				StorageIO.writeToFile(this.mMassMsgInfo.strFileName, this.mDatabuf, false);
			}
			if (this.mMassMsgInfo.nMsgType == 3u || this.mMassMsgInfo.nMsgType == 43u)
			{
				StorageIO.writeToFile(this.mMassMsgInfo.strThumbFileName, this.mThumbDatabuf, false);
			}
			return true;
		}
	}
}
